function get_goods_info() {
    return JSON.parse(localStorage.getItem('goods_info')) || [];
}

function save_data(data) {
    localStorage.setItem('goods_info', JSON.stringify(data))
}

function cart_goods() {
    var item = {};
    get_goods_info().forEach(function (good) {
        if (item[good.name]) item[good.name] += 1;
        else item[good.name] = good.count || 1;
    });
    return item;
}

function buy_goods_count(data) {
    var item = [];
    data.forEach(function (good) {
        delete good.id;
        delete good.created_at;
        delete good.updated_at;
        if (cart_goods()[good.name]) {
            good.count = cart_goods()[good.name];
            item.push(good);
        }
    });
    return item
}

function add_to_cart(good) {
    var item_info = get_goods_info();
    item_info.push(get_goods_names(good));
    save_data(item_info);
    save_data(buy_goods_count(get_cart_goods()));
    save_data(save_all_goods_info());
    cart_goods_count();
}

$(function () {
    get_all_count();
    get_cart_goods();
    get_inventory_count();
    hide_good_list();
    hide_tr();
});

function get_goods_names(good) {
    var item = {};
    item.sort = document.getElementById("sort" + good + "").innerText;
    item.name = document.getElementById("name" + good + "").innerText;
    item.unit = document.getElementById("unit" + good + "").innerText;
    item.price = document.getElementById("price" + good + "").innerText;
    return item
}

function save_goods(good) {
    var url = '/list/save_cart_goods';
    var data = good;
    var success = function (data) {
    };
    var error = function () {
        alert('n')
    };
    ajax_post(url, data, success, error)
}

function get_all_count() {
    var url = '/list/ajax_goods_count';
    var item;
    var success = function (data) {
        item = data;
    };
    var error = function () {
        alert('传参失败1')
    };
    get_ajax(url, success, error);
    $('#total_goods_number').text(item);
    return item
}

function get_cart_goods() {
    var url = '/list/ajax_goods';
    var item;
    var success = function (data) {
        item = data;
    };
    var error = function () {
        alert('传参失败2')
    };
    get_ajax(url, success, error);
    return item
}

function get_inventory_count() {
    var url = '/list/click_get_goods';
    var item;
    var success = function (data) {
        item = data;
    };
    var error = function () {
        alert('传参失败3')
    };
    get_ajax(url, success, error);
    return item
}

function get_goods_inventory() {
    var item = {};
    get_inventory_count().forEach(function (good) {
        item[good.name] = good.count;
    });
    return item
}

function hide_good_list() {
    get_cart_goods().forEach(function (good) {
        if (get_goods_inventory()[good.name] - good.count == 0) $('#' + good.name + 'tr_list').hide();
    })
}

function tr_hide_info(name) {
    get_cart_goods().forEach(function (good) {
        if (name == good.name && get_goods_inventory()[name] - good.count == 0) {
            $('#' + good.name + 'tr_list').hide();
            alert('已没有库存');
        }
    })
}

function hide_tr() {
    for (var key in get_goods_inventory()) {
        if (get_goods_inventory()[key] == 0) $('#' + key + 'tr_list').hide();
    }
}

function AddToCard(good) {
    save_goods(get_goods_names(good));
    get_all_count();
}
